/*    */ package com.cr.framework.shiro.service;
/*    */ 
/*    */ import com.cr.common.core.domain.entity.SysUser;
/*    */ import com.cr.common.exception.user.UserPasswordNotMatchException;
/*    */ import com.cr.common.exception.user.UserPasswordRetryLimitExceedException;
/*    */ import com.cr.common.utils.MessageUtils;
/*    */ import com.cr.framework.manager.AsyncManager;
/*    */ import com.cr.framework.manager.factory.AsyncFactory;
/*    */ import java.util.concurrent.atomic.AtomicInteger;
/*    */ import javax.annotation.PostConstruct;
/*    */ import org.apache.shiro.cache.Cache;
/*    */ import org.apache.shiro.cache.CacheManager;
/*    */ import org.apache.shiro.crypto.hash.Md5Hash;
/*    */ import org.springframework.beans.factory.annotation.Autowired;
/*    */ import org.springframework.beans.factory.annotation.Value;
/*    */ import org.springframework.stereotype.Component;
/*    */ 
/*    */ @Component
/*    */ public class SysPasswordService {
/*    */   @Autowired
/*    */   private CacheManager cacheManager;
/*    */   
/*    */   private Cache<String, AtomicInteger> loginRecordCache;
/*    */   
/*    */   @Value("${user.password.maxRetryCount}")
/*    */   private String maxRetryCount;
/*    */   
/*    */   @PostConstruct
/*    */   public void init() {
/* 39 */     this.loginRecordCache = this.cacheManager.getCache("loginRecordCache");
/*    */   }
/*    */   
/*    */   public void validate(SysUser user, String password) {
/* 44 */     String loginName = user.getLoginName();
/* 46 */     AtomicInteger retryCount = (AtomicInteger)this.loginRecordCache.get(loginName);
/* 48 */     if (retryCount == null) {
/* 50 */       retryCount = new AtomicInteger(0);
/* 51 */       this.loginRecordCache.put(loginName, retryCount);
/*    */     } 
/* 53 */     if (retryCount.incrementAndGet() > Integer.valueOf(this.maxRetryCount).intValue()) {
/* 55 */       AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginName, "Error", MessageUtils.message("user.password.retry.limit.exceed", new Object[] { this.maxRetryCount }), new Object[0]));
/* 56 */       throw new UserPasswordRetryLimitExceedException(Integer.valueOf(this.maxRetryCount).intValue());
/*    */     } 
/* 59 */     if (!matches(user, password)) {
/* 61 */       AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginName, "Error", MessageUtils.message("user.password.retry.limit.count", new Object[] { retryCount }), new Object[0]));
/* 62 */       this.loginRecordCache.put(loginName, retryCount);
/* 63 */       throw new UserPasswordNotMatchException();
/*    */     } 
/* 67 */     clearLoginRecordCache(loginName);
/*    */   }
/*    */   
/*    */   public boolean matches(SysUser user, String newPassword) {
/* 73 */     return user.getPassword().equals(encryptPassword(user.getLoginName(), newPassword, user.getSalt()));
/*    */   }
/*    */   
/*    */   public void clearLoginRecordCache(String loginName) {
/* 78 */     this.loginRecordCache.remove(loginName);
/*    */   }
/*    */   
/*    */   public String encryptPassword(String loginName, String password, String salt) {
/* 83 */     return (new Md5Hash(loginName + password + salt)).toHex();
/*    */   }
/*    */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-framework-4.7.0.jar!\com\cr\framework\shiro\service\SysPasswordService.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */